package cn.yunyichina.provider.message.service.impl;

import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.message.constant.MsgPushConstant;
import cn.yunyichina.provider.message.dao.MsgTempDao;
import cn.yunyichina.provider.message.dao.MsgTemplateContentDao;
import cn.yunyichina.provider.message.dao.MsgTemplateDao;
import cn.yunyichina.provider.message.dao.MsgTemplateFunctionDao;
import cn.yunyichina.provider.message.entity.MsgTemp;
import cn.yunyichina.provider.message.entity.MsgTemplate;
import cn.yunyichina.provider.message.entity.MsgTemplateContent;
import cn.yunyichina.provider.message.entity.MsgTemplateFunction;
import cn.yunyichina.provider.message.vo.MsgPushHospitalVO;
import cn.yunyichina.utils.convert.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 模板消息service实现类
 */
@Service
public class SysTemplateServiceImpl {

	@Autowired
	private MsgTemplateDao msgTemplateDao;

	@Autowired
	private MsgTemplateContentDao msgTemplateContentDao;

	@Autowired
	private MsgTemplateFunctionDao msgTemplateFunctionDao;

	@Autowired
	private MsgTempDao msgTempDao;


	/**
	 * 获取消息模版医院列表,后台管理
	 * */
	public ResponseObject getSysTemplateHospList(String data ) {
		Map<String,Object> params = JsonUtils.parseObject(data, Map.class);
		List<MsgPushHospitalVO> list =  msgTemplateDao.findHospListByPage(params);
		ResponseObject response = new ResponseObject();
		response.setResultCodeAndMessage("0", "调用成功");
		response.setResultObject(list);
		return response;
	}

	/**
	 * 获取消息模板列表，后台管理
	 * @param data
	 * @return
     */
	public ResponseObject ListSysTemplate(String data ) {
		Map<String,Object> params = JsonUtils.parseObject(data, Map.class);
		params.put("hospitalId",params.get("hospital_id"));
		List<MsgTemp> list =  msgTempDao.findListByPage(params);
		ResponseObject response = new ResponseObject();
		response.setResultCodeAndMessage("0", "调用成功");
		response.setResultObject(list);
		return response;
	}


	/**
	 * 保存消息模板
	 * @param data
	 * @return
     */
	public ResponseObject saveSysTemplate(String data){
		ResponseObject response = new ResponseObject();
		MsgTemplate entity = JsonUtils.parseObject(data,MsgTemplate.class);
		boolean status = true;
		if (StringUtils.isNotBlank(entity.getId())) {
			update(entity);
		} else {
			String id = add(entity);
			if (id == null) {
				status = false;
			}
		}
		if (status) {
			response.setResultCodeAndMessage("0", "保存成功");
		} else {
			response.setResultCodeAndMessage("-1", "申请模板失败!");
		}
		return response;
	}

	public String add(MsgTemplate entity) {
		String appId = null;
//		if (entity.getSource() == 3) {
//			HospitalCache hospitalCache = SpringContextHolder.getBean(HospitalCache.class);
//			HospIdAndAppSecretVo hospIdAndAppSecretVo = hospitalCache.getEasyHealthAppByHospitalId(entity.getHospitalId());
//			appId = hospIdAndAppSecretVo.getAppId();
//		} else {
//			HospitalCodeAndAppVo hospitalCodeAndAppVo =
//					baseDatasManager.getAppInfoByHospitalId(entity.getHospitalId(), entity.getSource() == 1 ? CacheConstant.CACHE_PAY_TYPE_WEIXIN
//							: CacheConstant.CACHE_PAY_TYPE_ALIPAY);
//			appId = hospitalCodeAndAppVo.getAppId();
//		}

		boolean status = true;
		// 暂时只做微信的模板申请
		if (entity.getSource() == 1) {
			String templateId = applyTemplate(appId, entity.getLibraryCode(), String.valueOf(entity.getSource()));
			if (templateId != null) {
				entity.setTemplateId(templateId);
			} else {
				status = false;
			}
		}
		if (status) {
			entity.setAppId(appId);
			msgTemplateDao.insert(entity);
			for (MsgTemplateContent msgTemplateContent : entity.getMsgTemplateContents()) {
				msgTemplateContent.setTemplateId(entity.getId());
				msgTemplateContentDao.insert(msgTemplateContent);
			}
			List<MsgTemplateFunction> functions = entity.getMsgTemplateFunctions();
			if (functions != null && functions.size() > 0) {
				for (MsgTemplateFunction msgTemplateFunction : functions) {
					msgTemplateFunction.setTemplateId(entity.getId());
					msgTemplateFunctionDao.insert(msgTemplateFunction);
				}
			}

//			msgManager.updateMsgTemplate(entity);更新缓存
			return entity.getId();
		}
		return null;

	}

	/**
	 * 申请模板
	 * @param appId
	 * @param libraryCode
	 * @param source
     * @return
     */
	public String applyTemplate(String appId, String libraryCode, String source) {
		String platformType = Integer.valueOf(source) == 1 ? MsgPushConstant.MODE_TYPE_WEIXIN : MsgPushConstant.MODE_TYPE_ALIPAY;
//		HospitalCodeAndAppVo hospitalCodeAndAppVo = baseDatasManager.queryHospitalCodeByApp(platformType, appId);
		String privateKey = "";//hospitalCodeAndAppVo.getPrivateKey();TODO: 2016/9/14 微信公众号privateKey
		String templateId = "";// todo:调用pay子系统  YXW.getTemplateId(appId, privateKey, platformType, libraryCode);
		return templateId;
	}

	public void update(MsgTemplate entity) {
		msgTemplateDao.update(entity);
		msgTemplateContentDao.deleteByTemplateId(entity.getId());
		msgTemplateFunctionDao.deleteByTemplateId(entity.getId());
		for (MsgTemplateContent msgTemplateContent : entity.getMsgTemplateContents()) {
			msgTemplateContent.setTemplateId(entity.getId());
			msgTemplateContentDao.insert(msgTemplateContent);
		}
		for (MsgTemplateFunction msgTemplateFunction : entity.getMsgTemplateFunctions()) {
			msgTemplateFunction.setTemplateId(entity.getId());
			msgTemplateFunctionDao.insert(msgTemplateFunction);
		}
//		msgManager.updateMsgTemplate(entity);
	}

}
